/*
 File: calendar-gdatepicker.js
 Grails g:datePicker binding to the
 DHTML Calendar www.dynarch.com/projects/calendar
 version history:
1.0  2009-03-28 Initial version (Michal Novak)

 Copyright 2009, Michal Novak, bubbles.way@gmail.com
 http://it-bubbles.blogspot.com/

 This script is distributed under the GNU Lesser General Public License.
 Read the entire license text here: http://www.gnu.org/licenses/lgpl.html

 This script is base on examples on following page:
 http://www.dynarch.com/static/jscalendar-1.0/index.html 
 */

// return value given by index of combo box element or null if combo box element is null
function comboGetValueAt(comboBoxElem, index) {
        if (comboBoxElem != null) {
                if (comboBoxElem.options.length > index) {
                        return comboBoxElem.options[index].value;
                }
        }
        return null;
}

// return selected value of combo box element  or null if combo box element is null
function comboSelectedValue(comboBoxElem) {
        if (comboBoxElem != null) {                
                return comboGetValueAt(comboBoxElem, comboBoxElem.selectedIndex)
        }
        return null;
}

// select value in combo box element (if combo box element is not null)
function selectCombo(comboBoxElem, value) {
        if (comboBoxElem != null) {
                for (var i = 0; i < comboBoxElem.options.length; i++) {
                        if (comboBoxElem.options[i].value == value &&
                            comboBoxElem.options[i].value != "") { //empty string is for "noSelection handling as "" == 0 in js
                                comboBoxElem.options[i].selected = true;
                                break
                        }
                }
        }
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
        selectCombo(cal.g_year, cal.date.getFullYear())
        selectCombo(cal.g_month, cal.date.getMonth() + 1)
        selectCombo(cal.g_day, cal.date.getDate())
        selectCombo(cal.g_hour, cal.date.getHours())
        selectCombo(cal.g_minute, cal.date.getMinutes())
        if (cal.dateClicked)
        //close the calendar on single-click.
                cal.callCloseHandler();
}

// This function gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button. It just hides the calendar without destroying it.
function closeHandler(cal) {
        cal.hide()                       // hide the calendar
        _dynarch_popupCalendar = null
}

// This function shows the calendar under the year element of the g:datePicker
// first parameter is id (name) of g:datePicker element
// second parameter (optional) specifies if time should be displayed in 24h format (by default yes)
// other necessary configuration is extracted directly from g:datePicker
function showCalendar(datePickerId, time24) {
        if (_dynarch_popupCalendar != null) { // we already have some calendar created
                _dynarch_popupCalendar.hide() // so we hide it first.
        }
        // create the calendar
        _dynarch_popupCalendar = new Calendar(1, null, selected, closeHandler)
        // remember grails date picker elements
        _dynarch_popupCalendar.g_year = document.getElementById(datePickerId + '_year')
        _dynarch_popupCalendar.g_month = document.getElementById(datePickerId + '_month')
        _dynarch_popupCalendar.g_day = document.getElementById(datePickerId + '_day')
        _dynarch_popupCalendar.g_hour = document.getElementById(datePickerId + '_hour')
        _dynarch_popupCalendar.g_minute = document.getElementById(datePickerId + '_minute')
        _dynarch_popupCalendar.showsTime = _dynarch_popupCalendar.g_hour != null // display time only if grails has hour element
        _dynarch_popupCalendar.time24 = time24 == null || time24
        // find out year range from grails date picker
        var minYear = comboGetValueAt(_dynarch_popupCalendar.g_year, 0) // get first item
        var maxYear = comboGetValueAt(_dynarch_popupCalendar.g_year, _dynarch_popupCalendar.g_year.options.length - 1) // get first item
        _dynarch_popupCalendar.setRange(parseInt(minYear), parseInt(maxYear)) // min/max year allowed.
        _dynarch_popupCalendar.create()
        var yearValue = comboSelectedValue(_dynarch_popupCalendar.g_year)
        var monthValue = comboSelectedValue(_dynarch_popupCalendar.g_month)
        var dayValue = comboSelectedValue(_dynarch_popupCalendar.g_day)
        var hourValue = comboSelectedValue(_dynarch_popupCalendar.g_hour)
        var minuteValue = comboSelectedValue(_dynarch_popupCalendar.g_minute)
        // day and month may not be present in grails date picker and if set to null, it makes calendar to break
        monthValue = monthValue == null || monthValue == "" ? 0 : parseInt(comboSelectedValue(_dynarch_popupCalendar.g_month)) - 1
        dayValue = dayValue == null ? 1 : comboSelectedValue(_dynarch_popupCalendar.g_day)
        var ourDate = new Date(yearValue, monthValue, dayValue, hourValue, minuteValue)
        // handle grails date picker "noSelection: in year, month or day combo box, in this case set value to today
        if (ourDate.getDate() != dayValue || ourDate.getMonth() != monthValue || ourDate.getFullYear() != yearValue) {
                ourDate = new Date()
        }
        _dynarch_popupCalendar.setDate(ourDate)
        _dynarch_popupCalendar.showAtElement(_dynarch_popupCalendar.g_year) // show the calendar        
}

